package com.liyujie.dyeingmachine.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.mobile.entity.ReagentEntity;
import com.liyujie.dyeingmachine.mobile.event.MessageEvent;
import com.liyujie.dyeingmachine.mobile.event.MessageType;
import com.liyujie.dyeingmachine.utils.InitializeUtils;
import com.liyujie.dyeingmachine.views.NoDoubleClickListener;
import com.lxj.xpopup.core.CenterPopupView;

import org.greenrobot.eventbus.EventBus;

public class AddReagentPopup extends CenterPopupView {
    /**
     * 试剂
     */
    private ReagentEntity mReagentEntity;

    public AddReagentPopup(@NonNull Context context) {
        super(context);
    }

    public AddReagentPopup(@NonNull Context context, ReagentEntity mReagentEntity) {
        super(context);
        this.mReagentEntity = mReagentEntity;

    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_add_reagebt;

    }


    @Override
    protected void onCreate() {
        super.onCreate();
        /**
         * 试剂名称
         */
        EditText mReagentName = findViewById(R.id.ReagentName);
        if (null != mReagentEntity) {
            mReagentName.setText(mReagentEntity.ReagentName);
        }
        /**
         * 染色时间
         */
        EditText mDyeingTime = findViewById(R.id.DyeingTime);
        if (null != mReagentEntity) {
            mDyeingTime.setText(mReagentEntity.ReagentDyeingTime + "");
        }
        /**
         * 取消
         */
        findViewById(R.id.ButtonCancel).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                /**
                 * 关闭
                 */
                dismiss();
            }
        });

        /**
         * 确认添加
         */
        findViewById(R.id.ConfirmAddition).setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                /**
                 * 试剂名称
                 */
                String StringReagentName = mReagentName.getText().toString();
                if (TextUtils.isEmpty(StringReagentName)) {
                    InitializeUtils.getDefaultMaker().show("请输入试剂名称");
                    return;
                }
                /**
                 * 染色时间
                 */
                String StringDyeingTime = mDyeingTime.getText().toString();
                if (TextUtils.isEmpty(StringDyeingTime)) {
                    InitializeUtils.getDefaultMaker().show("请输入染色时间(分钟)");
                }
                /**
                 *
                 */
                if (null != mReagentEntity) {
                    //试剂名称
                    mReagentEntity.ReagentName = StringReagentName;
                    //试剂染色时间
                    mReagentEntity.ReagentDyeingTime = Integer.parseInt(StringDyeingTime);
                    //更新
                    if (mReagentEntity.update()) {

                        /**
                         * 通知刷新
                         */
                        EventBus.getDefault().post(new MessageEvent(MessageType.OPEN_SUCCESSFILLY));
                        /**
                         * 试剂名称
                         */
                        mReagentName.setText("");
                        /**
                         * 染色时间
                         */
                        mDyeingTime.setText("");
                        /**
                         *
                         */
                        dismiss();
                    }

                } else {
                    ReagentEntity mReagentEntity = new ReagentEntity();
                    //试剂名称
                    mReagentEntity.ReagentName = StringReagentName;
                    //试剂染色时间
                    mReagentEntity.ReagentDyeingTime = Integer.parseInt(StringDyeingTime);
                    //试剂创建时间
                    mReagentEntity.ReagentCreationTime = TimeUtils.getNowMills();
                    //添加
                    if (mReagentEntity.save()) {
                        /**
                         * 通知刷新
                         */
                        EventBus.getDefault().post(new MessageEvent(MessageType.OPEN_SUCCESSFILLY));
                        /**
                         * 试剂名称
                         */
                        mReagentName.setText("");
                        /**
                         * 染色时间
                         */
                        mDyeingTime.setText("");
                    }
                }
            }
        });
    }
}
